package com.ruoyi.common.security.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.ruoyi.common.security.interceptor.HeaderInterceptor;

import start.spring.basic.util.common.StringUtil;

/**
 * 拦截器配置
 *
 * @author ruoyi
 */
public class WebMvcConfig implements WebMvcConfigurer
{
    /** 公共不需要拦截地址 */
    public static final String[] excludeUrls = { "/login", "/logout", "/refresh" };
    
    /** 单个服务中不需要拦截地址，多个地址以英文“,”分隔   */
    @Value("${exclude.valid.paths:}")
    private String excludeValidPath;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
    	String[]  excludeValidPaths = null;
    	if(!StringUtil.isEmpty(excludeValidPath)) {
    		excludeValidPaths = excludeValidPath.split(",");
    	}
    	
    	String[] allExcludePathPatterns = excludeUrls;
    	if(excludeValidPaths!=null) {
    		allExcludePathPatterns = new String[excludeUrls.length + excludeValidPaths.length];
    		System.arraycopy(excludeUrls, 0, allExcludePathPatterns, 0, excludeUrls.length);
    		System.arraycopy(excludeValidPaths, 0, allExcludePathPatterns, excludeUrls.length, excludeValidPaths.length);
    		
    	}
    	
        registry.addInterceptor(getHeaderInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(allExcludePathPatterns)
                .order(-10);
    }

    /**
     * 自定义请求头拦截器
     */
    public HeaderInterceptor getHeaderInterceptor()
    {
        return new HeaderInterceptor();
    }
}
